package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Optional;
import com.jnzc.ocr.common.utils.BigDecimalUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.serv.shop.consts.ShopOrderConsts;
import com.jnzc.ocr.serv.shop.entity.ShopMember;
import com.jnzc.ocr.serv.shop.entity.ShopOrder;
import com.jnzc.ocr.serv.shop.entity.ShopOrderRecharge;
import com.jnzc.ocr.serv.shop.filter.ShopOrderRechargeFilter;
import com.jnzc.ocr.serv.shop.helper.ShopOrderHelper;
import com.jnzc.ocr.serv.shop.helper.ShopOrderRechargeHelper;
import com.jnzc.ocr.serv.shop.mapper.ShopOrderRechargeMapper;
import com.jnzc.ocr.serv.shop.service.ShopMemberService;
import com.jnzc.ocr.serv.shop.service.ShopOrderRechargeService;
import com.jnzc.ocr.serv.shop.service.ShopOrderService;
import com.jnzc.ocr.serv.shop.vo.ShopOrderRechargeCreateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author zhangcm
 * @version 1.0, 2017-10-06
 */
@Service
public class ShopOrderRechargeServiceImpl extends MybatisServiceImpl<ShopOrderRechargeMapper, ShopOrderRecharge, ShopOrderRechargeFilter> implements ShopOrderRechargeService {

    @Autowired
    private ShopMemberService shopMemberService;

    @Autowired
    private ShopOrderService shopOrderService;

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopOrderRecharge> ew, ShopOrderRechargeFilter filter) {
        ShopOrderRecharge entity = new ShopOrderRecharge();
        entity.setOrderId(filter.getOrderId());
        entity.setShopId(filter.getShopId());
        ew.setEntity(entity);
    }

    @Override
    public ShopOrderRecharge find(Long orderId, Long shopId) {
        ShopOrderRechargeFilter filter = new ShopOrderRechargeFilter();
        filter.setOrderId(orderId);
        filter.setShopId(shopId);
        return super.findNotNull(filter);
    }

    @Override
    @Transactional
    public ShopOrder create(ShopOrderRechargeCreateVo createVo) {
        // 检查
        Long shopId = createVo.getOrder().getShopId();
        ValidateUtils.isId(shopId, "店铺ID");
        Long memberId = createVo.getOrder().getMemberId();
        ValidateUtils.isId(memberId, "会员ID");
        BigDecimal rechargeAmount = createVo.getRecharge().getRechargeAmount();
        ValidateUtils.inRange(rechargeAmount, new BigDecimal(1), "充值金额");

        String orderName = ShopOrderRechargeHelper.getOrderName(createVo.getRecharge());
        BigDecimal discountAmount = Optional.fromNullable(createVo.getRecharge().getDiscountAmount()).or(BigDecimal.ZERO);

        ShopOrder order = new ShopOrder();
        order.setShopId(shopId);
        order.setNo(ShopOrderHelper.createNo(shopId));
        order.setName(orderName);
        order.setMemberId(memberId);
        ShopMember member = shopMemberService.find(memberId, shopId);
        order.setMemberName(member.getName());
        order.setType(ShopOrderConsts.TYPE_RECHARGE);
        order.setUserId(createVo.getOrder().getUserId());
        order.setUserName(createVo.getOrder().getUserName());
        order.setAmount(BigDecimalUtils.sub(rechargeAmount, discountAmount));
        order.setAddPoint(0);
        order.setNum(1);
        order.setState(ShopOrderConsts.STATE_UNPAY);
        order.setCreateTime(DateUtils.getNow());
        shopOrderService.insertOne(order);

        ShopOrderRecharge recharge = new ShopOrderRecharge();
        recharge.setOrderId(order.getId());
        recharge.setShopId(shopId);
        recharge.setRechargeAmount(rechargeAmount);
        recharge.setDiscountAmount(discountAmount);
        this.insertOne(recharge);
        return order;
    }
}
